//后端主入口文件

//第一步，引入express
const express = require('express')
//实例化express
const app = express()
//定义端口号常量
const PORT = 5002
//连接数据库
require('./models/db')

//引入body-parser,用来解决post请求返回的body
const bodyParser = require('body-parser')
//引入users
const users = require('./routes/users')
//引入users
const roles = require('./routes/roles')
//引入categorys
const categorys = require('./routes/categorys')
//引入categorys
const products = require('./routes/products')
//引入passport用来进行身份验证
const passport = require('passport')

// 声明使用静态中间件,要不上传的图片打不开
app.use(express.static('public'))

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())

//passport初始化
app.use(passport.initialize());
require('./config/passport')(passport)

//使用users.js
app.use('/api', users)
//使用roles.js
app.use('/api', roles)
//使用categorys.js
app.use('/api', categorys)
//使用products.js
app.use('/api', products)

app.get('/', (req, res) => {
  res.send('Hello World!')
})


app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`)
}) 